package com.example.administrator.project_mould;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;

/**
 * Created by Administrator on 2017/3/13.
 */

public class ClassTableFragment extends Fragment {

    private GridView mGridview;

    /**模拟要显示的数据*/
    String[] data = {
             "", "", "", "", "", "", "",
             "语文", "", "", "", "", "", "",
             "语文", "", "英语", "", "", "", "",
             "", "数学", "英语", "", "", "", "",
             "", "体育", "", "", "", "", "",
             "", "", "", "", "化学", "", "",
             "", "", "", "", "", "", "",
             "", "", "", "", "", "", "",
             "", "", "", "", "", "", ""};
    private int mH;
    private LinearLayout mLl;
    private View mView;
    private int mLheight;
    private int mLlViewHeight;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //加载布局
        mView = inflater.inflate(R.layout.fragment_class_table, container, false);

        //初始化控件
        initView();

        return mView;
    }

    private void initView() {

        //课程显示控件
        mGridview = (GridView) mView.findViewById(R.id.gridview);


        //设置适配器
        mGridview.setAdapter(new GridViewAdapter());
    }

    private class GridViewAdapter extends BaseAdapter {

        private Button mBt;

        @Override
        public int getCount() {
            return data.length;
        }

        @Override
        public Object getItem(int position) {

            return data[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            /**做convertview的复用操作,课表内容显示不出来,滑动后才能正常显示出来 ????
             * 不使用convertview的复用操作,显示的更流畅???
             * */

            if (convertView==null){
                convertView=View.inflate(getActivity(),R.layout.class_item,null);

                //----------------------------------------------
                mBt = (Button) convertView.findViewById(R.id.tv_class_item);
                mBt.setText(data[position]);
                mBt.setHeight(mH);
                if (!data[position].equals("")){    //有课
                    mBt.setBackground(getResources().getDrawable(R.drawable.button_class_selector));
                }else {                             //没课
                    mBt.setBackground(getResources().getDrawable(R.drawable.button_class2_selector));
                }
                //----------------------------------------------

                convertView.setTag(new ViewHolder(convertView));
            }else {
                ViewHolder holder = (ViewHolder) convertView.getTag();
                /*Button btitem = holder.mBtitem;
                btitem.setText(data[position]);*/
            }
            return convertView;
        }
    }

    //viewholder
    protected class ViewHolder {
        private Button mBtitem;

        public ViewHolder(View view){
            mBtitem = (Button) view.findViewById(R.id.tv_class_item);
        }
    }

    //获取gridview每行的高度
    public void getHeight() {
        ViewTreeObserver vto = mGridview.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mGridview.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                //gridview的高度
                int height = mGridview.getHeight();
                Log.i("gridviewHeight",height+"");
                //获取行数
                int rowls = (int) Math.ceil(data.length / mGridview.getNumColumns());
                Log.i("gridviewRowls",rowls+"");
                //每行高
                mH = height / rowls-1 ;
                Log.i("rowlsHeight",mH+"");
            }
        });
    }
}


